class ArticleController {

    static allowedMethods = [save: "POST", update: "POST"]

    def authenticateService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        if (params.type) {
            def results = Article.withCriteria {
                and{
                    eq('category', params.type)
                    or{
                        eq('editor', User.get(authenticateService.userDomain()?.id) )
                        receiver {
                            eq('id', authenticateService.userDomain()?.id)
                        }
                        eq('broadcast', true)
                    }
                }
                maxResults(params.max) 
                order("lastTime", "desc")

            }
            def counts = Article.withCriteria {
                and{
                    eq('category', params.type)
                    or{
                        eq('editor', User.get(authenticateService.userDomain()?.id) )
                        receiver {
                            eq('id', authenticateService.userDomain()?.id)
                        }
                        eq('broadcast', true)
                    }
                }
                count('id')
            }
             [articleInstanceList: results , 
                articleInstanceTotal: counts ,
                id : authenticateService.userDomain()?.id ,
                type: params.type]
        }else{
            [articleInstanceList: null ,articleInstanceTotal: 0 ]
        }
    }

    def create = {
        def articleInstance = new Article()
        articleInstance.properties = params
        if (params.type) {
            articleInstance.category = params.type
        }
        return [articleInstance: articleInstance]
    }

    def save = {
        def articleInstance = new Article(params)

        

        articleInstance.editor = User.get(authenticateService.userDomain()?.id) 
        if (articleInstance.save(flush: true)) {
            if (articleInstance.informToMobile || articleInstance.informToMail) {
                sendJMSMessage("queue.notification", "create,"+articleInstance.id)
                log.info "article.save sendJMSMessage ${articleInstance.id}"
            }

            flash.message = "${message(code: 'default.created.message', args: [message(code: 'article.label', default: 'Article'), articleInstance])}"
            redirect(action: "list" , params: ['type': params.category])
        }
        else {
            render(view: "create", model: [articleInstance: articleInstance])
        }
    }

    def show = {
        def articleInstance = Article.get(params.id)
        def user = User.get(authenticateService.userDomain()?.id)

        if (!articleInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'article.label', default: 'Article'), params.id])}"
            redirect(action: "list")
        }
        else {
            if (!articleInstance.receiver?.isEmpty()){  
                if (articleInstance.receiver.contains(user) || articleInstance.editor == user) {
                    if (!Feedback.findBySenderAndReceiver(articleInstance , user )) {
                    new Feedback(      sender: articleInstance , receiver : user ).save()
                    }
                }else{
                    flash.message = "你无权查看这个文件。"
                    return 
                }
                
            }
            articleInstance.hit++
            articleInstance.save()
            [articleInstance: articleInstance]
        }
    }

    def edit = {
        def articleInstance = Article.get(params.id)
        if (!articleInstance) {
            
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'article.label', default: 'Article'), params.id])}"
            redirect(action: "list")
        }
        else {
            if (articleInstance.editor != User.get(authenticateService.userDomain()?.id)) {
                flash.message = "只有文件编辑人才能修改！"
                redirect(action: "list" , params: ['type' : articleInstance.category])
            }
            return [articleInstance: articleInstance]
        }
    }

    def update = {
        def articleInstance = Article.get(params.id)
        if (articleInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (articleInstance.version > version) {
                    
                    articleInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'article.label', default: 'Article')] as Object[], "Another user has updated this Article while you were editing")
                    render(view: "edit", model: [articleInstance: articleInstance])
                    return
                }
            }
            articleInstance.properties = params
            articleInstance.lastTime = new Date()
            if (!articleInstance.hasErrors() && articleInstance.save(flush: true)) {
                if (articleInstance.informToMobile || articleInstance.informToMail) {
                    sendJMSMessage("queue.notification", "edit,"+articleInstance.id)
                    log.info "article.save sendJMSMessage ${articleInstance.id}"
                }
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'article.label', default: 'Article'), articleInstance])}"
                redirect(action: "list" , params: ['type': params.category])
            }
            else {
                render(view: "edit", model: [articleInstance: articleInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'article.label', default: 'Article'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def articleInstance = Article.get(params.id)
        if (articleInstance) {
            try {
                articleInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'article.label', default: 'Article'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'article.label', default: 'Article'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'article.label', default: 'Article'), params.id])}"
            redirect(action: "list")
        }
    }
}
